
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>简易购物车 | home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="VUE的架构12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758&amp;lt;template&amp;gt;    &amp;lt;div&amp;gt;        &amp;lt;!-- 购物车 --&amp;gt;        &amp;lt;table&amp;gt;            &amp;lt;tr&amp;gt;                &amp;lt;td&amp;gt;商品名称&amp;lt;&amp;#x2F;td&amp;gt;                &amp;lt;td&amp;gt;商品数量&amp;lt;&amp;#x2F;td&amp;gt;         ...">
    
        <link rel="icon" href="/favicon.ico">
    
    
        
            <link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
        
            <link rel="stylesheet" href="/css/stage.css">
        
            <link rel="stylesheet" href="/css/avatar-bg.css">
        
    
<meta name="generator" content="Hexo 4.2.0"></head>

<body>
<header id="header">
    <div class="menu">
        <i class="fa fa-bars"></i>
    </div>
    <div class="header-main">
        <h1><a href="/">home</a></h1>
    </div>
    <div id="nav">
        <div class="nav-img" id="nav-img"></div>
        <div class="sentences">
            少年易学老难成，一寸光阴不可轻。——朱熹
        </div>
    </div>
</header>

<div id="content-outer">
    <div id="content-inner">
        <div class="clearfix">
    <article id="post">
        <h1>简易购物车</h1>
        <div class="create">
            <span>Created</span>
            
                <time datetime="2020-04-20T17:32:11.000Z">
                    2020-04-21
                </time>
            
            
        </div>
        <h1 id="VUE的架构"><a href="#VUE的架构" class="headerlink" title="VUE的架构"></a>VUE的架构</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;!-- 购物车 --&gt;</span><br><span class="line">        &lt;table&gt;</span><br><span class="line">            &lt;tr&gt;</span><br><span class="line">                &lt;td&gt;商品名称&lt;&#x2F;td&gt;</span><br><span class="line">                &lt;td&gt;商品数量&lt;&#x2F;td&gt;</span><br><span class="line">                &lt;td&gt;商品价格&lt;&#x2F;td&gt;</span><br><span class="line">            &lt;&#x2F;tr&gt;</span><br><span class="line">            &lt;tr v-for&#x3D;&quot;(item,index) in tlist&quot;&gt;</span><br><span class="line">                &lt;td&gt;&#123;&#123; item.text &#125;&#125;&lt;&#x2F;td&gt;</span><br><span class="line">                &lt;td&gt;</span><br><span class="line">                    &lt;button @click&#x3D;&quot;minus(index)&quot;&gt;+&lt;&#x2F;button&gt;</span><br><span class="line">                    &lt;input type&#x3D;&quot;text&quot; v-model&#x3D;&quot;item.num&quot;&gt;</span><br><span class="line">                    &lt;button @click&#x3D;&quot;add(index)&quot;&gt;+&lt;&#x2F;button&gt;</span><br><span class="line">                &lt;&#x2F;td&gt;</span><br><span class="line">                &lt;td&gt;&#123;&#123; item.price &#125;&#125;&lt;&#x2F;td&gt;</span><br><span class="line">            &lt;&#x2F;tr&gt;</span><br><span class="line">        &lt;&#x2F;table&gt;</span><br><span class="line">        &lt;p&gt;总数量:&#123;&#123;totalcount()&#125;&#125;        总价格:&#123;&#123;pricecount()&#125;&#125;&lt;&#x2F;p&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        &lt;button @click&#x3D;&quot;counter++&quot;&gt;+&lt;&#x2F;button&gt;</span><br><span class="line">        &#123;&#123;counter&#125;&#125;</span><br><span class="line">        &lt;button @click&#x3D;&quot;counter--&quot;&gt;-&lt;&#x2F;button&gt;</span><br><span class="line">        </span><br><span class="line">        &#123;&#123;reverse_msg&#125;&#125;</span><br><span class="line">        &lt;ul&gt;</span><br><span class="line">            &lt;li v-for&#x3D;&quot;(item,index) in tlist&quot;:class&#x3D;&quot;&#123;on:index%2&#x3D;&#x3D;0,off:index%2!&#x3D;0&#125;&quot;&gt;</span><br><span class="line">                &#123;&#123;item.text&#125;&#125;</span><br><span class="line">            &lt;&#x2F;li&gt;</span><br><span class="line">        &lt;&#x2F;ul&gt;</span><br><span class="line"></span><br><span class="line">        &lt;div v-html&#x3D;&quot;msg&quot;&gt;&lt;&#x2F;div&gt;</span><br><span class="line">        &#123;&#123;msg&#125;&#125;</span><br><span class="line">        &lt;h1&gt;&#123;&#123;msg&#125;&#125;&lt;&#x2F;h1&gt;  </span><br><span class="line">        &lt;h2 v-show&#x3D;&quot;ok&quot;&gt;Hello world&lt;&#x2F;h2&gt;</span><br><span class="line">        &lt;h2 v-if&#x3D;&quot;ok&quot;&gt;Hello world&lt;&#x2F;h2&gt;</span><br><span class="line"></span><br><span class="line">        &lt;div v-if&#x3D;&quot;type &#x3D;&#x3D;&#x3D; &#39;A&#39;&quot;&gt;A&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div v-else-if&#x3D;&quot;type &#x3D;&#x3D;&#x3D; &#39;B&#39;&quot;&gt;B&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div v-else&gt;不是A也不是B&lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">    &#x2F;&#x2F;定义数据</span><br><span class="line">    data()&#123;</span><br><span class="line">        return&#123;</span><br><span class="line">            msg:&#39;这是一个变量&#39;,</span><br><span class="line">            ok:0,</span><br><span class="line">            type:&#39;A&#39;,</span><br><span class="line">            tlist:[&#123;text:&#39;汽车&#39;,num:1,price:200&#125;,&#123;text:&#39;化妆品&#39;,num:1,price:50&#125;,&#123;text:&#39;衣服&#39;,num:1,price:30&#125;,&#123;text:&#39;牛奶&#39;,num:1,price:2&#125;],</span><br><span class="line">            counter:0</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></figure>
<h1 id="计算属性"><a href="#计算属性" class="headerlink" title="计算属性"></a>计算属性</h1><h3 id="1、字符串反转"><a href="#1、字符串反转" class="headerlink" title="1、字符串反转"></a>1、字符串反转</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;计算属性</span><br><span class="line">computed:&#123;</span><br><span class="line">    reverse_msg:function()&#123;</span><br><span class="line">        console.log(this.msg.split(&#39;&#39;));</span><br><span class="line">        return this.msg.split(&#39;&#39;).reverse().join(&#39;&#39;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h3 id="2、将变量的值每次更改打印到控制台"><a href="#2、将变量的值每次更改打印到控制台" class="headerlink" title="2、将变量的值每次更改打印到控制台"></a>2、将变量的值每次更改打印到控制台</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">watch:&#123;</span><br><span class="line">    counter:function(nval,oval)&#123;</span><br><span class="line">        console.log(&#39;计算器由&#39;+oval+&#39;变换为新的&#39;+nval)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br><span class="line">&#x2F;&#x2F;钩子方法  created</span><br><span class="line">mounted:function()&#123;</span><br><span class="line"></span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h1 id="自定义方法"><a href="#自定义方法" class="headerlink" title="自定义方法"></a>自定义方法</h1><h3 id="汇总数量，遍历tlist，将每条的num参数依次累加，得出总数量"><a href="#汇总数量，遍历tlist，将每条的num参数依次累加，得出总数量" class="headerlink" title="汇总数量，遍历tlist，将每条的num参数依次累加，得出总数量"></a>汇总数量，遍历tlist，将每条的num参数依次累加，得出总数量</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;自定义方法</span><br><span class="line">methods:&#123;</span><br><span class="line">    &#x2F;&#x2F;汇总数量</span><br><span class="line">    totalcount:function()&#123;</span><br><span class="line">        let total&#x3D;0;</span><br><span class="line">        for(let i&#x3D;0,l&#x3D;this.tlist.length;i&lt;l;i++)&#123;</span><br><span class="line">            total +&#x3D; this.tlist[i].num;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        return total;</span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></figure>

<h3 id="汇总价格，遍历tlist，将每条的num参数和price相乘后得到单品总价，然后相加到pricenum变量中，得出总价格"><a href="#汇总价格，遍历tlist，将每条的num参数和price相乘后得到单品总价，然后相加到pricenum变量中，得出总价格" class="headerlink" title="汇总价格，遍历tlist，将每条的num参数和price相乘后得到单品总价，然后相加到pricenum变量中，得出总价格"></a>汇总价格，遍历tlist，将每条的num参数和price相乘后得到单品总价，然后相加到pricenum变量中，得出总价格</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;汇总价格</span><br><span class="line">pricecount:function()&#123;</span><br><span class="line">    let pricenum&#x3D;0;</span><br><span class="line">    for(let i&#x3D;0,l&#x3D;this.tlist.length;i&lt;l;i++)&#123;</span><br><span class="line">        pricenum +&#x3D; this.tlist[i].price*this.tlist[i].num;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    return pricenum;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h3 id="购物车数量的减少，this-tlist-index-num-gt-0判断数量是否大于0，如果大于0则可以减，如果小于0，不改变"><a href="#购物车数量的减少，this-tlist-index-num-gt-0判断数量是否大于0，如果大于0则可以减，如果小于0，不改变" class="headerlink" title="购物车数量的减少，this.tlist[index].num&gt;0判断数量是否大于0，如果大于0则可以减，如果小于0，不改变"></a>购物车数量的减少，this.tlist[index].num&gt;0判断数量是否大于0，如果大于0则可以减，如果小于0，不改变</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;购物车减法</span><br><span class="line">minus:function(index)&#123;</span><br><span class="line">    if(this.tlist[index].num&gt;0)&#123;</span><br><span class="line">        this.tlist[index].num--</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        this.tlist[index].num</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h3 id="购物车数量的增加，将下标传下来，将数量进行增加，一次加1"><a href="#购物车数量的增加，将下标传下来，将数量进行增加，一次加1" class="headerlink" title="购物车数量的增加，将下标传下来，将数量进行增加，一次加1"></a>购物车数量的增加，将下标传下来，将数量进行增加，一次加1</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">        &#x2F;&#x2F;购物车加法</span><br><span class="line">        add:function(index)&#123;</span><br><span class="line">            this.tlist[index].num++</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line"></span><br><span class="line">.on &#123;background-color: blue;color:thistle;font-size: 20px&#125;</span><br><span class="line">.off &#123;background-color: red;color: black;font-size: 30px&#125;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>

<h1 id="vue-中的方法"><a href="#vue-中的方法" class="headerlink" title="vue 中的方法"></a>vue 中的方法</h1><h3 id="v-html-””-展示变量"><a href="#v-html-””-展示变量" class="headerlink" title="v-html=”” 展示变量"></a>v-html=”” 展示变量</h3><h3 id="v-show-””-如果是True则显示，如果是Flase则不显示"><a href="#v-show-””-如果是True则显示，如果是Flase则不显示" class="headerlink" title="v-show=”” 如果是True则显示，如果是Flase则不显示"></a>v-show=”” 如果是True则显示，如果是Flase则不显示</h3><h3 id="v-if-”type-’’”判断条件满足时显示"><a href="#v-if-”type-’’”判断条件满足时显示" class="headerlink" title="v-if=”type===’’”判断条件满足时显示"></a>v-if=”type===’’”判断条件满足时显示</h3><h3 id="v-else-if-”type-‘’”判断满足条件时显示"><a href="#v-else-if-”type-‘’”判断满足条件时显示" class="headerlink" title="v-else-if=”type === ‘’”判断满足条件时显示"></a>v-else-if=”type === ‘’”判断满足条件时显示</h3><h3 id="v-else-如果条件都不满足则显示"><a href="#v-else-如果条件都不满足则显示" class="headerlink" title="v-else  如果条件都不满足则显示"></a>v-else  如果条件都不满足则显示</h3><h3 id="v-for-”-item-index-in-tlist”-遍历tlist，提取其中的item和index"><a href="#v-for-”-item-index-in-tlist”-遍历tlist，提取其中的item和index" class="headerlink" title="v-for=”(item,index) in tlist” 遍历tlist，提取其中的item和index"></a>v-for=”(item,index) in tlist” 遍历tlist，提取其中的item和index</h3><p><img src="/hugo_blog/5ba58572111d481045b9894394b50afe.jpg" alt="美图"><br><img src="/hugo_blog/5e531bfe76e1831109f79c4222d865c0.jpg" alt="美图"><br><img src="/hexo_blog/34339465.jpg" alt="美图"></p>

        <div>
            <ul class="tags-category-list" itemprop="keywords"><li class="tags-category-list-item"><a class="tags-category-list-link" href="/hexo_blog/tags/python/" rel="tag">python</a></li></ul>
        </div>
        <div class="bottom-line"></div>
        
    <nav id="article-nav">
        
            <a href="/hexo_blog/2020/04/21/Django/" id="article-nav-newer" class="article-nav-link-wrap">
        <span class="article-nav-title">
            
                Django
            
        </span>
                <strong class="article-nav-caption">&gt;</strong>
            </a>
        
        
    </nav>


        
    </article>
    <div id="toc">
        
            <h2>Table of Contents</h2>
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#VUE的架构"><span class="toc-number">1.</span> <span class="toc-text">VUE的架构</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#计算属性"><span class="toc-number">2.</span> <span class="toc-text">计算属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1、字符串反转"><span class="toc-number">2.0.1.</span> <span class="toc-text">1、字符串反转</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2、将变量的值每次更改打印到控制台"><span class="toc-number">2.0.2.</span> <span class="toc-text">2、将变量的值每次更改打印到控制台</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#自定义方法"><span class="toc-number">3.</span> <span class="toc-text">自定义方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#汇总数量，遍历tlist，将每条的num参数依次累加，得出总数量"><span class="toc-number">3.0.1.</span> <span class="toc-text">汇总数量，遍历tlist，将每条的num参数依次累加，得出总数量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#汇总价格，遍历tlist，将每条的num参数和price相乘后得到单品总价，然后相加到pricenum变量中，得出总价格"><span class="toc-number">3.0.2.</span> <span class="toc-text">汇总价格，遍历tlist，将每条的num参数和price相乘后得到单品总价，然后相加到pricenum变量中，得出总价格</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#购物车数量的减少，this-tlist-index-num-gt-0判断数量是否大于0，如果大于0则可以减，如果小于0，不改变"><span class="toc-number">3.0.3.</span> <span class="toc-text">购物车数量的减少，this.tlist[index].num&gt;0判断数量是否大于0，如果大于0则可以减，如果小于0，不改变</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#购物车数量的增加，将下标传下来，将数量进行增加，一次加1"><span class="toc-number">3.0.4.</span> <span class="toc-text">购物车数量的增加，将下标传下来，将数量进行增加，一次加1</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#vue-中的方法"><span class="toc-number">4.</span> <span class="toc-text">vue 中的方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#v-html-””-展示变量"><span class="toc-number">4.0.1.</span> <span class="toc-text">v-html&#x3D;”” 展示变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-show-””-如果是True则显示，如果是Flase则不显示"><span class="toc-number">4.0.2.</span> <span class="toc-text">v-show&#x3D;”” 如果是True则显示，如果是Flase则不显示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-if-”type-’’”判断条件满足时显示"><span class="toc-number">4.0.3.</span> <span class="toc-text">v-if&#x3D;”type&#x3D;&#x3D;&#x3D;’’”判断条件满足时显示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-else-if-”type-‘’”判断满足条件时显示"><span class="toc-number">4.0.4.</span> <span class="toc-text">v-else-if&#x3D;”type &#x3D;&#x3D;&#x3D; ‘’”判断满足条件时显示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-else-如果条件都不满足则显示"><span class="toc-number">4.0.5.</span> <span class="toc-text">v-else  如果条件都不满足则显示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-for-”-item-index-in-tlist”-遍历tlist，提取其中的item和index"><span class="toc-number">4.0.6.</span> <span class="toc-text">v-for&#x3D;”(item,index) in tlist” 遍历tlist，提取其中的item和index</span></a></li></ol></li></ol></li></ol>
        
    </div>
</div>

    </div>
</div>
<footer id="footer">
    <div id="copyright">&copy; Mr.Song  2020</div>
    <div id="theme">
        Powered by <a href="http://hexo.io" target="_blank" rel="noopener">Hexo</a>. Theme by <a href="https://github.com/markyong/hexo-theme-stage" target="_blank" rel="noopener">Stage</a>
    </div>
</footer>
<script src="/lib/js/waterrippleeffect.min.js"></script>
<script src="/js/header-bg.main.js"></script>

    <script src="/lib/js/smooth-scroll.min.js"></script>
    <script src="/js/toc.main.js"></script>

</body>
</html>
